package com.oxy.action;

import java.util.List;

import org.apache.commons.logging.Log;

import com.oxy.common.constant.ActionConstant;
import com.oxy.action.util.ActionUtil;
import com.oxy.model.Pager;
import com.oxy.model.po.MemberPO;
import com.oxy.svc.MemberSvc;
import com.oxy.util.Oxy;
import com.oxy.util.SysErrException;

public class MemberAction extends BaseAction {
	private final static Log logger = Oxy.getLogManager().getLogCommon();
	
	public String index() {
		return ActionConstant.RESULT.SUCCESS;
	}

	public String list() {
		String ret = ActionConstant.RESULT.SUCCESS;

		MemberSvc svc = (MemberSvc)this.getSvcHolder().getSvcByIfClass(MemberSvc.class);
		
		Pager pager = ActionUtil.preparePager(svc.getMemberCount());
		this.getParaOut().put(ActionConstant.PARA_OUT.COMMON.PAGER, pager);
		
		List<MemberPO> list = null;
		list = svc.list(pager);
		this.getParaOut().put(ActionConstant.PARA_OUT.MEMBER.MEMBER_LIST, list);
		
		return ret;
	}
	
	public String login() {
		return ActionConstant.RESULT.SUCCESS;
	}
	
	public String doLogin() {
		String result = ActionConstant.RESULT.ERROR;
		
		String email = ActionUtil.getRequestParam("email");
		String password = ActionUtil.getRequestParam("password");
		if(email == null || email.equals("")) {
			this.getParaOut().put(ActionConstant.PARA_OUT.COMMON.ERROR_MSG, "email is null");
			return ActionConstant.RESULT.ERROR;
		}
		if(password == null || password.equals("")) {
			this.getParaOut().put(ActionConstant.PARA_OUT.COMMON.ERROR_MSG, "password is null");
			return ActionConstant.RESULT.ERROR;
		}
		
		MemberSvc svc = (MemberSvc)this.getSvcHolder().getSvcByIfClass(MemberSvc.class);
		if(svc.loginByEmail(email, password) == true) {
			result = ActionConstant.RESULT.SUCCESS;
		}
		else {
			this.getParaOut().put(ActionConstant.PARA_OUT.COMMON.ERROR_MSG, "Login failed");
			result = ActionConstant.RESULT.ERROR;
		}
		
		return result;
	}
	
	public String doLogout() {
		ActionUtil.rmSessionAttr(ActionConstant.SESSION.CURR_USER_INFO);
		return ActionConstant.RESULT.SUCCESS;
	}
	
	public String register() {
		return ActionConstant.RESULT.SUCCESS;
	}
	public String doRegister() throws SysErrException {
		String email = ActionUtil.getRequestParam("email");
		String password = ActionUtil.getRequestParam("password");
		if(email == null || email.equals("") || password == null || password.equals("")) {
			this.getParaOut().put(ActionConstant.PARA_OUT.COMMON.ERROR_MSG, "提交资料不完整");
			return ActionConstant.RESULT.ERROR;
		}
		MemberSvc svc = (MemberSvc)this.getSvcHolder().getSvcByIfClass(com.oxy.svc.MemberSvc.class);
		MemberPO mem = new MemberPO();
		mem.setEmail(email);
		mem.setPassword(password);
		long uid = svc.regMember(mem);
		logger.info("New user inserted, return = "+uid+", uid = "+mem.getUid());
		
		return ActionConstant.RESULT.SUCCESS;
	}
	
	public String profile() {
		return ActionConstant.RESULT.SUCCESS;
	}
	public String saveProfile() {
		return ActionConstant.RESULT.SUCCESS;
	}
}
